package com.tim.app.ui.adapter.selceted;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.application.library.net.StringResponseCallback;
import com.application.library.widget.recycle.BaseRecyclerAdapter;
import com.application.library.widget.roundimg.RoundedImageView;
import com.google.gson.Gson;
import com.tim.app.R;
import com.tim.app.constant.AppKey;
import com.tim.app.server.api.API_User;
import com.tim.app.server.entry.ActionStateMsg;
import com.tim.app.server.entry.Topic;
import com.tim.app.server.logic.UserManager;
import com.tim.app.server.result.FollowResult;
import com.tim.app.server.result.ResponseResult;
import com.tim.app.util.BitmapLoader;
import com.tim.app.util.ToastUtil;
import com.tim.app.util.ViewGT;

import java.util.List;


/**
 * 擅长话题
 */
public class GoodAtTopicAdapter extends BaseRecyclerAdapter<BaseRecyclerAdapter.BaseRecyclerViewHolder, Topic> {

    public static final String TAG = "GoodAtTopicAdapter";

    private Context mContext;

    private boolean isShowStateButton;

    public GoodAtTopicAdapter(Context mContext, List<Topic> mDataList, boolean isShowStateButton) {
        super(mDataList);
        this.mContext = mContext;
        this.isShowStateButton = isShowStateButton;
    }

    @Override
    public BaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        BaseRecyclerViewHolder holder = null;
        holder = new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.cell_good_at_topic, null));
        return holder;
    }

    @Override
    public void onBindViewHolder(BaseRecyclerViewHolder mHolder, final int position, final Topic data) {
        if (data == null) {
            return;
        }
        final ViewHolder holder = (ViewHolder) mHolder;
        if (!TextUtils.isEmpty(data.getImage())) {
            BitmapLoader.ins().loadImage(data.getImage(),  holder.rivSubject);
        }else{
            BitmapLoader.ins().loadImage("",  holder.rivSubject);
        }
        if (!TextUtils.isEmpty(data.getTopicName())) {
            holder.tvSubjectTitle.setText(data.getTopicName());
        }
        if (!TextUtils.isEmpty(data.getDigest())) {
            holder.tvSubjectSubTitle.setVisibility(View.VISIBLE);
            holder.tvSubjectSubTitle.setText(data.getDigest());
        } else {
            holder.tvSubjectSubTitle.setVisibility(View.GONE);
        }
        if (isShowStateButton) {
            holder.tvFollowSubject.setVisibility(View.VISIBLE);
        } else {
            holder.tvFollowSubject.setVisibility(View.GONE);
        }
        if (position == getDataList().size() - 1) {
            holder.vLine.setVisibility(View.GONE);
        } else {
            holder.vLine.setVisibility(View.VISIBLE);
        }
        if (null != data.getActionStateMsg()) {
            if (null != data.getActionStateMsg().getAdeptTopic()) {
                holder.tvFollowSubject.setSelected(true);
                holder.tvFollowSubject.setText(mContext.getString(R.string.hasAdd));
            } else {
                holder.tvFollowSubject.setSelected(false);
                holder.tvFollowSubject.setText(mContext.getString(R.string.unAdd));
            }
        }
        holder.tvFollowSubject.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!UserManager.ins().isLogin()){
                    ViewGT.gotoLoginActivity(mContext);
                    return;
                }
                addOrCancelGoodAt(data, holder.tvFollowSubject, position);
            }
        });
    }

    /**
     * 擅长或者取消擅长
     * @param data
     * @param tvFollowSubject
     * @param position
     */
    private void addOrCancelGoodAt(final Topic data, final TextView tvFollowSubject, final int position) {
        tvFollowSubject.setEnabled(false);
        if (null != data.getActionStateMsg() && data.getActionStateMsg().getAdeptTopic() != null) {
            int id = data.getActionStateMsg().getAdeptTopic();
            API_User.ins().cancelFollow(TAG, id, UserManager.ins().getUserId(), data.getId(), AppKey.TYPE_GOOD_AT, new StringResponseCallback() {
                @Override
                public boolean onStringResponse(String resultValue, int errCode, String errMsg, int id, boolean formCache) {
                    tvFollowSubject.setEnabled(true);
                    if (errCode == ResponseResult.ResultStatus.OK && resultValue != null) {
                        data.getActionStateMsg().setAdeptTopic(null);
                        getDataList().set(position, data);
                        notifyItemChanged(position);
                    } else {
                        ToastUtil.showToast(errMsg);
                    }
                    return false;
                }
            });
        } else {
            API_User.ins().addFollow(TAG, UserManager.ins().getUserId(), data.getId(), AppKey.TYPE_GOOD_AT, new StringResponseCallback() {
                @Override
                public boolean onStringResponse(String resultValue, int errCode, String errMsg, int id, boolean formCache) {
                    tvFollowSubject.setEnabled(true);
                    if (errCode == ResponseResult.ResultStatus.OK && resultValue != null) {
                        FollowResult result = new Gson().fromJson(resultValue, FollowResult.class);
                        if (null != result && null != result.getRes()) {
                            if ( getDataList().get(position).getActionStateMsg() == null) {
                                getDataList().get(position).setActionStateMsg(new ActionStateMsg());
                            }
                            data.getActionStateMsg().setAdeptTopic(result.getRes().getConcern().getId());
                        }
                        getDataList().set(position, data);
                        notifyItemChanged(position);
                    } else {
                        ToastUtil.showToast(errMsg);
                    }
                    return false;
                }
            });
        }
    }

    public class ViewHolder extends BaseRecyclerViewHolder {

        RoundedImageView rivSubject;
        TextView tvSubjectTitle, tvSubjectSubTitle;
        TextView tvFollowSubject;
        View vLine;

        public ViewHolder(View itemView) {
            super(itemView);
            rivSubject = (RoundedImageView) itemView.findViewById(R.id.rivSubject);
            tvSubjectTitle = (TextView) itemView.findViewById(R.id.tvSubjectTitle);
            tvSubjectSubTitle = (TextView) itemView.findViewById(R.id.tvSubjectSubTitle);
            tvFollowSubject = (TextView) itemView.findViewById(R.id.tvFollowSubject);
            vLine = itemView.findViewById(R.id.vLine);
        }

    }

}
